package com.nf.health.app.widget;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.customview.CustomShareBoard;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.SharePlatform;
import com.nf.health.app.widget.flake.FlakeView;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Ian on 2017/6/5.
 */
public class DialogActivity extends Activity {

    Boolean show = false;

    private final UMSocialService mController = UMServiceFactory
            .getUMSocialService(Constants.DESCRIPTOR);


    String count, url;
    @Bind(R.id.lin_anim)
    RelativeLayout linAnim;
    @Bind(R.id.iv_close)
    ImageView ivClose;
    @Bind(R.id.tv_count)
    TextView tvCount;
    @Bind(R.id.tv_gethealthcoin)
    LinearLayout tvGethealthcoin;
    @Bind(R.id.iv_shard)
    TextView ivShard;
    @Bind(R.id.linearLayout2)
    RelativeLayout linearLayout2;
    @Bind(R.id.container)
    LinearLayout container;
    @Bind(R.id.rl_content_view)
    RelativeLayout rlContentView;
    @Bind(R.id.icon1)
    ImageView icon1;
    @Bind(R.id.icon2)
    ImageView icon2;
    @Bind(R.id.icon3)
    ImageView icon3;
    @Bind(R.id.icon4)
    ImageView icon4;
    @Bind(R.id.icon5)
    ImageView icon5;
    @Bind(R.id.icon6)
    ImageView icon6;
    @Bind(R.id.icon7)
    ImageView icon7;
    @Bind(R.id.anim1)
    ImageView anim1;
    @Bind(R.id.anim2)
    ImageView anim2;
    @Bind(R.id.anim3)
    ImageView anim3;
    @Bind(R.id.anim4)
    ImageView anim4;
    @Bind(R.id.anim5)
    ImageView anim5;
    @Bind(R.id.anim6)
    ImageView anim6;
    @Bind(R.id.anim7)
    ImageView anim7;
    //金币掉落动画的主体动画
    private FlakeView flakeView, flakeView2;

    Animation animation,animation2,animation3,animation4,animation5,animation6,animation7;

    @Override
    protected void onCreate(Bundle arg0) {
        //金币掉落动画的主体动画
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        //此两段代码必须设置在setContentView()方法之前
        setContentView(R.layout.layout_red_shard_dialog);
        ButterKnife.bind(DialogActivity.this);

        //设置透明状态栏
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
        }

        animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation3 = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation4 = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation5 = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation6 = AnimationUtils.loadAnimation(this, R.anim.translate);
        animation7 = AnimationUtils.loadAnimation(this, R.anim.translate);

        anim1.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim7.startAnimation(animation7);
                anim1.setVisibility(View.INVISIBLE);
                icon1.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim6.startAnimation(animation6);
                anim2.setVisibility(View.INVISIBLE);
                icon2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation3.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim5.startAnimation(animation5);
                anim3.setVisibility(View.INVISIBLE);
                icon3.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation4.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim4.setVisibility(View.INVISIBLE);
                icon4.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });  animation5.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim4.startAnimation(animation4);
                icon5.setVisibility(View.VISIBLE);
                anim5.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation6.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim3.startAnimation(animation3);
                anim6.setVisibility(View.INVISIBLE);
                icon6.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });  animation7.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                anim2.startAnimation(animation2);
                anim7.setVisibility(View.INVISIBLE);
                icon7.setVisibility(View.VISIBLE);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });






        count = getIntent().getStringExtra("count");
        url = getIntent().getStringExtra("url");

        ViewGroup.LayoutParams params = rlContentView.getLayoutParams();
        params.width = DensityUtil.getScreenIntWidth(DialogActivity.this);
        params.height = DensityUtil.getScreenIntHeight(DialogActivity.this);
        rlContentView.setLayoutParams(params);

        flakeView = new FlakeView(this, "icon");
        flakeView2 = new FlakeView(this, "follow");
        TextView iv_shard = (TextView) findViewById(R.id.iv_shard);
        ImageView iv_close = (ImageView) findViewById(R.id.iv_close);
        TextView tv_count = (TextView) findViewById(R.id.tv_count);

        tv_count.setText(count);


        //将flakeView 添加到布局中
        container.addView(flakeView);
        container.addView(flakeView2);
        //设置背景
        //设置同时出现在屏幕上的金币数量  建议64以内 过多会引起卡顿
        flakeView.addFlakes(8);
        flakeView2.addFlakes(8);
        /**
         * 绘制的类型
         * @see View.LAYER_TYPE_HARDWARE
         * @see View.LAYER_TYPE_SOFTWARE
         * @see View.LAYER_TYPE_NONE
         */
        flakeView.setLayerType(View.LAYER_TYPE_NONE, null);
        flakeView2.setLayerType(View.LAYER_TYPE_NONE, null);

        /**
         * 移除动画
         */
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //设置2秒后
                    Thread.sleep(5000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            container.setVisibility(View.INVISIBLE);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        if (!show)
            thread.start();
        MediaPlayer player = MediaPlayer.create(this, R.raw.shake);
        player.start();
        /**
         * 移除动画
         */
        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //设置2秒后
                    Thread.sleep(5000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            container.setVisibility(View.INVISIBLE);
                            linearLayout2.setVisibility(View.VISIBLE);

                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        if (!show)
            thread2.start();
        MediaPlayer player2 = MediaPlayer.create(this, R.raw.shake);
        player2.start();


        iv_shard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomShareBoard shareBoard = new CustomShareBoard(DialogActivity.this);
                shareBoard.setbg();
                shareBoard.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                //再设置模式，和Activity的一样，覆盖，调整大小。
                shareBoard.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                shareBoard.showAtLocation(DialogActivity.this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
                shareBoard.showAtLocation(DialogActivity.this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
                //TODO
//                showBottomDialog();

                // 配置需要分享的相关平台
                configPlatforms();
                // 设置分享的内容
                setShareContent();
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void configPlatforms() {
        // 添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        SharePlatform.addQQ_QZonePlatform(DialogActivity.this);
        SharePlatform.addWXPlatform(DialogActivity.this);
    }

    private void setShareContent() {
        // 配置SSO
        String title = "我在参加【健康中国•治未病健康工程】,有专项健康管理，还能返钱哦！！",
                imgurl = "", content = "健康工程由中国中医药信息研究会健康管理与促进专业委员会牵头,以治未病科为依托，以移动互联网APP为工具，以物联网的检测设备为手段，以健康管理为核心，以健康服务业为支点，以健康小屋为执行点，实现全生命周期的实施管理，促进全民健康！", httpUrl = url, image = "";
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        UMImage umImage;
        if (TextUtils.isEmpty(image) || image == null) {//判断分享文章中的第一张图片是否为空
            umImage = new UMImage(DialogActivity.this, R.mipmap.ic_logo);//赋值默认logo
        } else {
            umImage = new UMImage(DialogActivity.this, image);
        }
        String targetUrl = httpUrl;
        String content1 = content;
        // 微信分享必须设置targetURL，需要为http链接格式
        // 微信朋友圈只能显示title，并且过长会被微信截取部分内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setShareContent(content1);
        weixinContent.setTitle(title);
        weixinContent.setTargetUrl(targetUrl);
        weixinContent.setShareMedia(umImage);
        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(content1);
        circleMedia.setTitle(title);
        circleMedia.setShareMedia(umImage);
        circleMedia.setTargetUrl(targetUrl);
        mController.setShareMedia(circleMedia);

        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(content1);
        qqShareContent.setTitle(title);
        qqShareContent.setShareMedia(umImage);
        qqShareContent.setTargetUrl(targetUrl);
        mController.setShareMedia(qqShareContent);
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setTargetUrl(targetUrl);
        qzone.setShareContent(content1);
        qzone.setTitle(title);
        qzone.setShareMedia(umImage);
        mController.setShareMedia(qzone);
        //新浪微博、腾讯微博及豆瓣的跳转链接只能设置在分享文字之中，以http形式传递即可，人人网可以单独设置跳转链接，方法为：
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setShareContent(title + httpUrl);
        //		sinaContent.setTitle(title);
        sinaContent.setShareImage(umImage);
        mController.setShareMedia(sinaContent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        flakeView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        flakeView.pause();
    }

}
